package com.adaydayup.week1;

import java.util.Arrays;
import java.util.HashMap;

/**
 * 文件的最长绝对路径(388)
 * <p>
 * 注意：\t 是一位
 * dir\n \t subdir1\n \t subdir2\n \t \t file.ext
 * dir
 * --->subdir1
 * --->subdir2
 * ----------->file.ext
 * <p>
 * 绝对路径：dir/subdir2/file.ext
 */
public class LengthLongestPath {
    public static int lengthLongestPath(String input) {
        int ans = 0;
        int lengthCount[] = new int[input.length()];
        String[] path = input.split("\n");//[dir, 	subdir1, 	subdir2, 		file.ext]
        System.out.println(Arrays.toString(path));
        for (int i = 0; i < path.length; i++) {
            int count = countTabs(path[i]);
            lengthCount[count + 1] = lengthCount[count] + path[i].length() - count;
            if (path[i].contains(".")) {
                ans = Math.max(ans, lengthCount[count + 1] + count);
            }
        }
        return ans;
    }

    private static int countTabs(String s) {
        //计算字符串前“\t”的数量
        int start = 0;
        while (start < s.length() && s.charAt(start) == '\t') {
            start++;
        }
        return start;
    }

    /*
    2 哈希表
     */
    public static int lengthLongestPath2(String input) {
        HashMap<Integer, String> map = new HashMap<>();
        int len = input.length();
        String ans = null;
        for (int i = 0; i < len; ) {
            int level = 0;
            while (i < len && input.charAt(i) == '\t' && ++level > 0) {
                i++;
            }
            int j=i;
            boolean isDir=true;
            while (j<len&&input.charAt(j)!='\n'){
                if (input.charAt(j++)=='.'){
                    isDir=false;
                }
            }
            String cur=input.substring(i,j);
//            String prev=map.getOrDefault();
        }
        return 0;
    }

    public static void main(String[] args) {
        String in = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext";
        int i = lengthLongestPath(in);
        System.out.println(i);
    }


}
